﻿using System;
using GameDesigner.Source.Widgets;
using GameLibrary.Model.Core;
using WeifenLuo.WinFormsUI.Docking;
using System.Windows.Forms;
using MDXInfo.Util.Script;
using G.Model.Runtime;
using GameLibrary.Model;

namespace GameDesigner.Source.Forms
{
    public partial class ScriptForm : DockContent
    {
        private GameScriptDef _Script;

        public ScriptForm()
        {
            InitializeComponent();
            ActiveObject.ObjectChangedEvent += OnObjectChanged;
            ActiveGame.GameChangedEvent += OnGameSelected;
        }
        
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
            ActiveObject.ObjectChangedEvent -= OnObjectChanged;
            ActiveGame.GameChangedEvent -= OnGameSelected;
        }

        private void OnGameSelected(GameDef game)
        {
            _Script = null;
            if (game == null)
            {
                _Editor.Text = "";
            }
        }
        
        void OnObjectChanged(object subject)
        {
            _Script = null;
            
            if (subject == null)
            {
                _Editor.Text = "";
                return;
            }

            Type t1 = subject.GetType();
            Type t2 = typeof(HasScriptDef);
            if (t2.IsAssignableFrom(t1))
            {
                HasScriptDef script = (HasScriptDef)subject;
                _Editor.Text = script.GameScriptDef.Text;
                _Script = script.GameScriptDef;
                _Editor.ReadOnly = false;
                _Editor.Modified = false;
            }
            else
            {
                _Editor.Text = "";
                _Editor.ReadOnly = true;
                _Editor.Modified = false;
            }
        }

        public GameScriptDef GameScriptDef
        {
            get { return _Script; }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (_Script != null)
            {
                try
                {
                    _Editor.Script.Compile(new CompilerOutputDelegate(this.HandleCompilerOutput));
                }
                catch (Exception exp)
                {
                    MessageBox.Show(this, "An error occurred compiling this script:\n\n" + exp.Message, 
                        "Error compiling script", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    System.Diagnostics.Debug.WriteLine(exp);
                }
            }
        }

        private void HandleCompilerOutput(string line)
        {
            Console.WriteLine(line);
        }

        void _EditorTextChangedEvent()
        {
            if (_Script != null)
            {
                _Script.Text = _Editor.Text;
            }
        }
    }
}
